package cn.livingCloud.service.impl;

import cn.livingCloud.dto.MemberDto;
import cn.livingCloud.dto.MemberInfoDto;
import cn.livingCloud.dto.MemberJsonDto;
import cn.livingCloud.dto.MemberListDto;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.MemberMapper;
import cn.livingCloud.repository.SettingMapper;
import cn.livingCloud.service.MemberService;
import cn.livingCloud.utils.DateUtils;
import cn.livingCloud.utils.SessionUtils;
import cn.livingCloud.utils.UuidGenerator;
import cn.livingCloud.wechat.protocol.WechatUserInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.cas.CasToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by DingJie  on 2015/12/29.
 */
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    protected MemberMapper memberMapper;
    @Autowired
    protected SettingMapper settingMapper;

    @Override
    public void loginByCode(String code) {
        Subject subject = SecurityUtils.getSubject();
        subject.login(new CasToken(code));
    }

    @Override
    public void load(MemberListDto memberListDto) {
        memberListDto.addAllList(memberMapper.getList(memberListDto));
    }

    @Override
    public MemberDto uploadState(String uuid, boolean state) {
        Member memberByUuid = memberMapper.findMemberByUuidNotState(uuid);
        //如果前台传入执行的状态和数据库的状态相同那么就抛出异常
        if (state == memberByUuid.isState()) {
            throw new IllegalStateException("禁用/启用失败,请检查账号状态是否被禁用/启用!");
        }
        //如果前台的状态和数据库的状态不相同那么就执行当前传入的操作
        memberByUuid.setState(state);
        memberMapper.update(memberByUuid);
        return new MemberDto(memberByUuid);
    }

    @Override
    public MemberInfoDto loadMemberInfoDto() {
        Member currentMember = (Member) SessionUtils.getCurrentMember();
        Member member = memberMapper.findMemberByUuid(currentMember.getUuid());
        return new MemberInfoDto(member);
    }

    @Override
    @Deprecated
    public Member createTestMember() {
        Member member;
        List<Member> list = memberMapper.getList(new MemberListDto());
        if (list.size() > 0) {
            member = list.get(0);
        } else {
            WechatUserInfo userInfo = new WechatUserInfo()
                    .setOpenid(UuidGenerator.generate())
                    .setNickname("test")
                    .setSex("1")
                    .setProvince("四川")
                    .setCity("成都")
                    .setCountry("中国")
                    .setHeadimgurl("/resources/images/user.png");
            member = new Member(userInfo)
                    .setState(true)
                    .setCreateTime(DateUtils.now());
            memberMapper.save(member);
        }
        return member;
    }

    @Override
    public MemberListDto findUnBoundMember(String key) {
        MemberListDto listDto = new MemberListDto();
        listDto.setNickname(key);
        listDto.addAllList(memberMapper.findUnBoundMemberList(listDto));
        return listDto;
    }

    @Override
    public List<MemberJsonDto> findMembers(String key) {
        if (StringUtils.isEmpty(key)) {
            return Collections.emptyList();
        }
        List<MemberJsonDto> result = new ArrayList<>();
        List<Member> members = memberMapper.searchMember(key);
        for (Member emp : members) {
            result.add(new MemberJsonDto(emp));
        }
        return result;
    }

    @Override
    public MemberListDto findUnbindMember(String key) {
        MemberListDto listDto = new MemberListDto();
        listDto.setNickname(key);
        listDto.addAllList(memberMapper.findUnBindMemberList(listDto));
        return listDto;
    }
}
